package com.gooluke.shorten.strategy;

import com.gooluke.shorten.context.GenerateContext;
import com.gooluke.shorten.util.Base62;
import com.gooluke.shorten.util.DistributedIdGenerator;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * 分布式ID+Base62生成短码
 */
@Component
@Primary
public class DistributedIdStrategy implements ShortCodeStrategy {

    // 分布式ID生成器（这里使用简化版Snowflake实现）
    private final DistributedIdGenerator idGenerator = new DistributedIdGenerator();
    // Base62编码器
    private final Base62 base62 = new Base62();

    @Override
    public String generate(String originalUrl, GenerateContext context) {
        long id = idGenerator.nextId();
        return base62.encode(id).substring(0, 8); // 取前8位
    }
}